package com.gather.system;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;

import com.work.vo.ThirdInfo;

public class AccountData {
	
	/**
	 * 已登录帐号
	 */
	public static ConcurrentMap<String, CopyOnWriteArrayList<ThirdInfo>> thirdInfos = new ConcurrentHashMap<String, CopyOnWriteArrayList<ThirdInfo>>();// 每个登录线程的名字和线程ID

	/**
	 * 帐号权重
	 */
	public static  ConcurrentMap<String,Integer> thirdinfoIndex  = new ConcurrentHashMap<String,Integer>();
	
	
	/**
	 * 当帐号失效时则登录帐号移除 待登录帐号加入
	 * @param thirdInfo
	 */
	public static void logBack(ThirdInfo thirdInfo){
		String webCode = thirdInfo.getWebConfig().getSnatchWeb();
		String userName = thirdInfo.getLoginAccount().getUserName();
		List<ThirdInfo> list = thirdInfos.get(webCode);
		for(int i = 0;i<list.size();i++){
			ThirdInfo temthirdInfo = list.get(i);
			if(temthirdInfo.getLoginAccount().getUserName().equals(userName)){
				list.remove(i);
			}
		}
		thirdInfo.getLoginAccount().setTryNum(0);
		thirdInfo.getLoginAccount().setLogin(false);
	}
	
	/**
	 * 加入待登录帐号
	 * @param webCode
	 * @param loginData
	 *//*
	public static void addLoginData(String webCode,LoginData loginData){
		CopyOnWriteArrayList<LoginData> list = loginDatas.get(webCode);
		if(null == list){
			list = new CopyOnWriteArrayList<LoginData>();
			loginDatas.put(webCode, list);
		}
		boolean inhere = false;
		for(int i=0;i<list.size();i++){
			LoginData temloginData = list.get(i);
				if(temloginData.getLoginAccount().getUserName().equals(loginData.getLoginAccount().getUserName())){
					inhere = true;
				}
			}
		if(!inhere){
			list.add(loginData);
		}
	}*/
	
	/**
	 * 加入已登录帐号
	 * @param webCode
	 * @param thirdInfo
	 */
	public static void addThirdInfo(String webCode,ThirdInfo thirdInfo){
		CopyOnWriteArrayList<ThirdInfo>list = thirdInfos.get(webCode);
		if(null == list){
			list = new CopyOnWriteArrayList<ThirdInfo>();
			thirdInfos.put(webCode, list);
		}
		boolean inhere = false;
		for(int i=0;i<list.size();i++){
			ThirdInfo temthirdInfo = list.get(i);
				if(temthirdInfo.getLoginAccount().getUserName().equals(thirdInfo.getLoginAccount().getUserName())){
					list.set(i, thirdInfo);
					inhere = true;
				}
			}
		if(!inhere){
			list.add(thirdInfo);
		}
	}

	/**
	 * 查询已登录帐号列表
	 * @param webCode
	 * @return
	 */
	public static List<String> validAccount(String webCode) {
		List<String> accounts = new ArrayList<String>();
		List<ThirdInfo> list = thirdInfos.get(webCode);
		if(list!=null){
			for(ThirdInfo thirdInfo : list){
				accounts.add(thirdInfo.getLoginAccount().getUserName());
			}
		}
		return accounts;
	}
	
	
	/**
	 * 获取一个可用帐号
	 * @param webCode
	 * @return
	 */
	public static ThirdInfo findThirdInfo(String webCode) {
		Integer index = thirdinfoIndex.get(webCode);
		if(null == index){
			index = 0;
			thirdinfoIndex.put(webCode, index);
		}
		List<ThirdInfo> list = thirdInfos.get(webCode);
		if(list==null){
			return null;
		}
		if(index >=(list.size()-1)){
			index = 0;
		}
		
		for(int i =0;i<list.size();i++){
			if(index >=(list.size()-1)){
				index = 0;
			}
			ThirdInfo thirdInfo = list.get(index);
			if(!thirdInfo.isSnatch() && thirdInfo.isValid()){
				thirdinfoIndex.put(webCode,++index);
				return thirdInfo;
			}
		}
		return null;
	}
	
	
}
